<?php

namespace App\Admin\Controllers\Common;

use App\Admin\Models\AdminUserRole;
use App\Admin\Services\AdminMenuRole;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Common\Services\Common;
use App\Common\Services\Upload;
use App\Common\Services\Admin\Common as AdminCommon;
use Illuminate\Support\Facades\Redis;

class CommonController extends Controller
{
    /**
     * 上传文件至OSS
     */
    public function uploadtooss(Request $request)
    {
        $res = Upload::uploadToOss($request);
        if (isset($res['code']) && $res['code'] == 0) {
            return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => ['path' => $res['path'], 'filename' => $res['filename']]], 200);
        } else {
            return response()->json(['code' => '001', 'msg' => isset($res['msg']) ? $res['msg'] : '上传失败'], 200);
        }
    }

    /**
     * 上传文件,常用于上传后下载、或者文件导入以及生成
     */
    public function uploadfiletolocal(Request $request)
    {
        $res = Upload::uploadFile($request);
        if (isset($res['code']) && $res['code'] == 0) {
            return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => ['path' => $res['path']]], 200);
        } else {
            return response()->json(['code' => '001', 'msg' => isset($res['msg']) ? $res['msg'] : '上传失败'], 200);
        }
    }

    /**
     * 获取省市区列表
     */
    public function getregion(Request $request){
        $pid=$request->pid ?? 0;
        $list=Common::getRegion($pid);
        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => ['list' => $list]]);
    }
    /**
     * 获取超管以及运维的菜单信息
     */
    public function getmenulist(Request $request){

        $info = AdminCommon::getCurrentAdmininfo($request);
        if($info['type']==1){
            $menulist = Redis::get('admin_user_menulist_Admin_0');

            if(!$menulist){
                AdminMenuRole::setAdminRole(0,1);
                $menulist = Redis::get('admin_user_menulist_Admin_0');
            }
            $menulist = json_decode($menulist, true);
        }else{
            $menulist = Redis::get('admin_user_menulist_Admin_' . $info['id']);
            if ($menulist && is_array(json_decode($menulist, true))) {
                $menulist = json_decode($menulist, true);
            } else {
                $rule_info = AdminUserRole::where(['admin_id' => $info['id']])->first();
                $menulist = json_decode($rule_info->ruleinfo, true);
                Redis::set('admin_user_menulist_Admin_' .$info['id'], $rule_info->ruleinfo);
            }

            foreach ($menulist as $key => $val) {
                if ($val['is_checked'] == 0) {
                    unset($menulist[$key]);
                } else {
                    if(isset($val['children'])){
                        foreach ($val['children'] as $kkey => $vval) {
                            if ($vval['is_checked'] == 0) {
                                unset($menulist[$key]['children'][$kkey]);
                            }else{
                                if(isset($vval['children'])){
                                    foreach ($vval['children'] as $kkkey => $vvval) {
                                        if ($vvval['is_checked'] == 0) {
                                            unset($menulist[$key]['children'][$kkey]['children'][$kkkey]);
                                        }else{
                                            if(isset($vvval['mate']['buttonList'])){
                                                foreach ($vvval['mate']['buttonList'] as $bbbkey => $bbbvval) {
                                                    if ($bbbvval['is_checked'] == 0) {
                                                        unset($menulist[$key]['children'][$kkey]['children'][$kkkey]['mate']['buttonList'][$bbbkey]);
                                                    }
                                                }
                                                $menulist[$key]['children'][$kkey]['children'][$kkkey]['mate']['buttonList']=array_values($menulist[$key]['children'][$kkey]['children'][$kkkey]['mate']['buttonList']);
                                            }
                                        }
                                        
                                    }
                                    $menulist[$key]['children'][$kkey]['children']=array_values($menulist[$key]['children'][$kkey]['children']);
                                }
    
                                if(isset($vval['mate']['buttonList'])){
                                    foreach ($vval['mate']['buttonList'] as $bbkey => $bbvval) {
                                        if ($bbvval['is_checked'] == 0) {
                                            unset($menulist[$key]['children'][$kkey]['mate']['buttonList'][$bbkey]);
                                        }
                                    }
                                    $menulist[$key]['children'][$kkey]['mate']['buttonList']=array_values($menulist[$key]['children'][$kkey]['mate']['buttonList']);
                                }
                            }
                            
                        }
                        $menulist[$key]['children']=array_values($menulist[$key]['children']);
                    }
    
                    if(isset($val['mate']['buttonList'])){
                        foreach ($val['mate']['buttonList'] as $bkey => $bvval) {
                            if ($bvval['mate']['is_checked'] == 0) {
                                unset($menulist[$key]['mate']['buttonList'][$bkey]);
                            }
                        }
                        $menulist[$key]['mate']['buttonList']=array_values($menulist[$key]['mate']['buttonList']);
                    }
                    
                }
            }
        }


        $menulist=array_values($menulist);
        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $menulist]);

    }
}